package cn.tanghom.support.interceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.rmi.server.RemoteServer;
import java.util.Set;

/**
 * 描述
 *
 * @author tanghom<tanghom@qq.com> 2016/4/20
 */
public class RmiSecurityInterceptor implements MethodInterceptor {
    private Set<String> allowed = null;

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        String clientHost = RemoteServer.getClientHost();
        if (allowed != null){
        	if(allowed.contains(clientHost)) {
        		return methodInvocation.proceed();
        	}
        	for(String item : allowed){
        		if(clientHost.matches(item)){
        		   return methodInvocation.proceed();
        		}
        	}
        	throw new SecurityException("非法访问。");
            
        } else {
            throw new SecurityException("非法访问。");
        }
    }

    public void setAllowed(Set<String> allowed) {
        this.allowed = allowed;
    }
}
